package com.example.travelservlet.city.controller;

import cn.hutool.log.Log;
import com.example.travelservlet.city.service.CityService;
import com.example.travelservlet.entity.City;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 30697
 */
@RestController
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityService cityService;

    @GetMapping("/findCity/{proId}")
    public Result<List<City>> findCity(@PathVariable String proId){
        List<City> cities = cityService.findCitiesByProId(proId);
        return ResultUtil.success(cities);
    }

    @GetMapping("/findCityById/{cityId}")
    public Result<City> findCityById(@PathVariable String cityId) {
        City city = cityService.findCityById(cityId);
        return ResultUtil.success(city);
    }

    @GetMapping("/findCityByName/{cityName}")
    public Result<String> findCityByName(@PathVariable String cityName) {
        City city = cityService.findCityByName(cityName);
        String cityId = city.getCityId();
        return ResultUtil.success(cityId);
    }

    @PostMapping("/createCity")
    public Result<City> createCity(@Valid String proId, @Valid String cityId, @Valid String cityName) {
        City city = new City();
        city.setCityId(cityId);
        city.setCityName(cityName);
        city.setProId(proId);
        City city1 = cityService.createCity(city);
        return ResultUtil.success(city1);
    }

    @GetMapping("/findAll")
    public List<String> findAll(){
        List<City> all = cityService.findALL();
        List<String> list = new ArrayList<>();
        for(City city : all){
            list.add(city.getCityName());
        }
        return list;
    }
}
